package com.garlic.face.jlf.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.presenter.SuperVisorConfirmGoodsContract;
import com.garlic.face.jlf.presenter.SuperVisorConfirmGoodsPresenter;
import com.garlic.face.jlf.transaction.bean.SaleFlow;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.Tools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by shiyanqi on 18/9/19.
 */

public class SuperVisorConfirmGoodsActivity extends BaseActivity implements SuperVisorConfirmGoodsContract.View, GoodsRecyclerViewAdapter.OnItemListener {

	SuperVisorConfirmGoodsPresenter mPresenter;
	private RecyclerView mRecyclerView;
	private GoodsRecyclerViewAdapter mAdapter = null;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_supervisor_confirm_goods);
		mPresenter = new SuperVisorConfirmGoodsPresenter(this);
		initView();
		initData();
	}

	private void initData() {
		processItemList();
		EventBus.getDefault().register(this);
	}

	private void initView() {

		findViewById(R.id.confirm_goods).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
		findViewById(R.id.cancel_pay).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				showProgress();
				mPresenter.cancelTransaction();
			}
		});
		mRecyclerView = findViewById(R.id.recyclerView);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
				DividerItemDecoration.VERTICAL));
		mRecyclerView.setAdapter(mAdapter);

	}

	@Override
	public void processItemList() {
		updateAdapter(mPresenter.getItemList());
	}

	@Override
	public void gotoMain() {
		App.clearToMainActivity();
	}

	private void updateAdapter(List<SaleFlow> saleFlowList) {
		if (mAdapter == null) {
			mAdapter = new GoodsRecyclerViewAdapter(SuperVisorConfirmGoodsActivity.this, saleFlowList);
			mRecyclerView.setAdapter(mAdapter);
			mRecyclerView.scrollToPosition(0);
			mAdapter.setOnItemListener(SuperVisorConfirmGoodsActivity.this);
		} else {
			mAdapter.setData(saleFlowList);
			mRecyclerView.scrollToPosition(0);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onReceiveResponse(JSONObject jsonObject) {
		LogUtils.d("收到service消息" + jsonObject.toString(), true);
		mPresenter.parseResponse(jsonObject);
	}

	@Override
	public void onItemDelete(int position) {
		View root = View.inflate(SuperVisorConfirmGoodsActivity.this, R.layout.delete_dialog, null);
		TextView itemName = root.findViewById(R.id.item_name);
		TextView itemPrice = root.findViewById(R.id.item_price);
		SaleFlow goodsItem = mPresenter.getItemList().get(position);
		if (goodsItem != null) {
			itemName.setText(""+goodsItem.getFname());
			itemPrice.setText("¥" + Tools.getFormatNumber(goodsItem.getPrice()));
		}

		//ImageOptions imageOptions = new ImageOptions.Builder()
		//        .setFadeIn(true)
		//        .setLoadingDrawableId(R.mipmap.commodity)
		//        .build();

		//x.image().bind(itemIcon, goodsItem.getGoods_img(),imageOptions);
		AlertDialog.Builder builder = new AlertDialog.Builder(SuperVisorConfirmGoodsActivity.this).setView(root)
				.setCancelable(false);
		final Dialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		root.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		root.findViewById(R.id.done).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showProgress();
				mPresenter.removeItem(position);
				dialog.dismiss();
			}
		});
	}

	@Override
	public void onItemAdd(int position) {
		showProgress();
		mPresenter.addItem(position);
	}

	@Override
	public void onItemMiuns(int position) {
		showProgress();
		mPresenter.reduceItem(position);
	}

	@Override
	protected void onPause() {
		super.onPause();
		EventBus.getDefault().unregister(this);
	}
}
